// map.js
import qqmap from '../../../utils/qqmap'

Page({
  data: {
    repairDetail: {},
    latitude: 40.060539,
    longitude: 116.343847,
    markers: [
      {
        id: 1,
        latitude: 40.060539,
        longitude: 116.343847,
      },
      {
        id: 2,
        latitude: 40.061539,
        longitude: 116.341847,
        iconPath: '/static/images/marker.png',
        width: 50,
        height: 50,
      },
    ],
    polyline: [],
  },
  onLoad({ id }) {
    this.getDetial(id)
    this.getPolyline()
  },
  // 获取报修详情
  async getDetial(id) {
    const res = await wx.http.get(`/repair/${id}`)
    this.setData({
      repairDetail: res.data,
    })
  },
  // 报修路径规划
  getPolyline() {
    qqmap.direction({
      mode: 'driving',
      from: { latitude: 40.061539, longitude: 116.341847 },
      to: { latitude: 40.060539, longitude: 116.343847 },
      success: (res) => {
        // polyline 坐标解压
        const coors = res.result.routes[0].polyline
        for (let i = 2; i < coors.length; i++) {
          coors[i] = coors[i - 2] + coors[i] / 1000000
        }
        //将解压后的坐标放入点串数组中
        const points = []
        for (let i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        this.setData({
          polyline: [
            {
              points,
              color: '#00d26a',
              width: 4,
            },
          ],
        })
      },
    })
  },
  // 修改报修信息
  editRepair(e) {
    wx.redirectTo({
      url: `/repair_pkg/pages/form/index?id=${e.mark.id}`,
    })
  },
  // 取消报修信息
  async cancelRepair(e) {
    await wx.http.put(`/cancel/repaire/${e.mark.id}`)
    wx.navigateTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
